# coding: utf-8

from flask import Flask
from flask import redirect
from flask import render_template

from restapis.AllSay import FarmerSay
from restapis.PublishSay import SayPub
from restapis.DelSay import DelSay
from restapis.FolloweeSay import FolloweeSay
from restapis.SubSay import SubSay
from views.todos import todos_view
from views.upload import upload_view
from restapis.FavorterSay import FavoriteSay
from restapis.CommentSay import CommentSay
from flask import request
from restapis.meeting import Meeting

app = Flask(__name__)

# 动态路由
app.register_blueprint(todos_view, url_prefix='/todos')
app.register_blueprint(upload_view, url_prefix='/upload')


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/farmsaypublish', methods=['POST'])
def publishFarmer():
    sayPub = SayPub()
    return sayPub.publish()


@app.route('/farmsayget', methods=['POST'])
def getAllSay():
    farmerSay = FarmerSay()
    return farmerSay.getAllSay()


@app.route('/newfarmsayget', methods=['POST'])
def getNewSay():
    farmerSay = FarmerSay()
    return farmerSay.getNewSays()


@app.route('/oldfarmsayget', methods=['POST'])
def getOldSay():
    farmerSay = FarmerSay()
    return farmerSay.getOldSays()


@app.route('/delFarmerSay', methods=['POST'])
def delFarmerSay():
    delSay = DelSay()
    return delSay.delFarmerSayItem()


@app.route('/delAllSay', methods=['POST'])
def delAllSay():
    delSay = DelSay()
    return delSay.delAll()


@app.route('/getsubnewsay', methods=['POST'])
def getSubNewSay():
    subSay = SubSay()
    return subSay.getSubNewSay()


@app.route('/getsuboldsay', methods=['POST'])
def getSubOldSay():
    subSay = SubSay()
    return subSay.getSubOldSay()


@app.route('/gettosub', methods=['POST'])
def getToSub():
    subSay = SubSay()
    return subSay.getToSub()


@app.route('/addFollowee', methods=['POST'])
def addFollowee():
    followeeSay = FolloweeSay()
    return followeeSay.addFollowee()


@app.route('/cancelFollowee', methods=['POST'])
def cancelFollowee():
    followeeSay = FolloweeSay()
    return followeeSay.cancelFollowee()


@app.route('/likeSay', methods=['POST'])
def likeSay():
    flag = request.json['flag']
    likeSay = FavoriteSay()
    if flag == 0:
        return likeSay.like()
    if flag == 1:
        return likeSay.unLike()


@app.route('/commentSay', methods=['POST'])
def commentSay():
    flag = request.json['flag']
    commentSay = CommentSay()
    if flag == 0:
        return commentSay.add()
    if flag == 1:
        return commentSay.delete()


@app.route('/update', methods=['POST'])
def updateMeeting():
    meet = Meeting()
    return meet.update()


@app.route('/query', methods=['POST'])
def query():
    meet = Meeting()
    return meet.query()

@app.route('/query', methods=['GET'])
def queryGet():
    meet = Meeting()
    return meet.queryGet()
